package com.nacos.test.controller;

import com.nacos.test.PersonService;
import com.nacos.test.pojo.Person;
import com.nacos.test.utils.JSONResult;
import com.nacos.test.vo.PersonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.*;

/**
 * 项目名称：nacos_parent
 * 类 名 称：PersonController
 * 类 描 述：TODO
 * 创建时间：2020/5/14
 * 创 建 人：Areli
 */
@Api(tags = "用户服务相关接口")
@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    PersonService personService;

    @Autowired
    private Registration registration;

    @ApiOperation("根据id查询用户接口")
    @GetMapping("/{id}")
    public JSONResult getPersonById(@ApiParam(name = "id",value = "用户id",required = true) @PathVariable("id") Integer id){
        Person person = personService.queryPersonByPersonId(id);
        PersonVO personVO = new PersonVO();
        BeanUtils.copyProperties(person,personVO);
        return JSONResult.ok(personVO);
    }

    @GetMapping("/getIPAndPort")
    public String getIPAndPort(){
        return registration.getHost()+":"+registration.getPort();
    }

    @PostMapping
    public JSONResult addPerson(@RequestBody Person person){
        Person personResult = personService.savePerson(person);
        PersonVO personVO = new PersonVO();
        BeanUtils.copyProperties(personResult,personVO);
        return JSONResult.ok(personVO);
    }

    @PutMapping
    public JSONResult editPerson(@RequestBody Person person){
        Person editPerson = personService.editPerson(person);
        PersonVO personVO = new PersonVO();
        BeanUtils.copyProperties(editPerson,personVO);
        return JSONResult.ok(personVO);
    }

    @DeleteMapping("/{id}")
    public String delPersonById(@PathVariable("id")Integer id){
        personService.deletePersonByPersonId(id);
        return "删除成功";
    }
}
